package io.renren.modules.vipcard.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.vipcard.dao.VipCardDao;
import io.renren.modules.vipcard.entity.VipCardEntity;
import io.renren.modules.vipcard.service.VipCardService;


@Service("vipCardService")
public class VipCardServiceImpl extends ServiceImpl<VipCardDao, VipCardEntity> implements VipCardService {


    //获取前一天
    private   String getafteDay(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date=null;
        date = new Date(time);
        calendar.setTime(date);
        int day=calendar.get(Calendar.DATE);
        //                      此处修改为+1则是获取后一天
        calendar.set(Calendar.DATE,day+1);

        String lastDay = sdf.format(calendar.getTime());
        return lastDay;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String isUserd= (String) params.get("isUsed");
        String searchDate= (String) params.get("searchDate[0]");
       String time0=null,time1=null;
        SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd");
       // SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        if(searchDate!=null) {
            Date date0 = new Date((String) params.get("searchDate[0]"));
            Date date1 = new Date((String) params.get("searchDate[1]"));

             time0 = format0.format(date0.getTime());//这个就是把时间戳经过处理得到期望格式的时间
             time1=getafteDay((String) params.get("searchDate[1]"));
        }
        Page<VipCardEntity> page = this.selectPage(
                new Query<VipCardEntity>(params).getPage(),
                new EntityWrapper<VipCardEntity>()
                .eq(StringUtils.isNotBlank(isUserd),"is_used", isUserd)
                .between(StringUtils.isNotBlank(searchDate),"create_date",time0,time1)

        );

        return new PageUtils(page);
    }

    @Override
    public void insertByNumber(Integer num) {
        List<VipCardEntity> list=new ArrayList();
    for(int i=0;i<num;i++){
        VipCardEntity vipCardEntity=new VipCardEntity();
        vipCardEntity.setPassword(UUID.randomUUID().toString().replaceAll("-", "").substring(0,20));
        vipCardEntity.setCreateDate(new Date());
        list.add(vipCardEntity);
    }
    System.out.println();
    this.insertBatch(list);
    }

}
